package njust.struts2.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import njust.dao.GoodsDao;
import njust.dao.ImageDao;
import njust.domain.Goods;
import njust.domain.Image;

import com.opensymphony.xwork2.ActionSupport;

public class ImageAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	ImageDao imageDao;
	String message;
	Image image;
	int goodId;

	GoodsDao goodDao;

	public String addImage() {
		Goods good = goodDao.getGoodById(goodId);
		if (good == null || image.getUrl().equals("")) {
			message = "不能为空";
			return "addImageError";
		}
		image.setGoods(good);

		if (imageDao.addImage(image)) {
			message = "添加成功";
			return "addImageSuccess";
		} else {
			message = "添加失败";
			return "addImageError";
		}
	}

	public String deleteImage(int id) {
		if (imageDao.deleteImage(id)) {
			message = "删除成功";
			return "deleteimageok";
		} else {
			message = "删除失败";
			return "deleteimageerror";
		}
	}

	public String getAllImage() {
		List images = new ArrayList();
		images = imageDao.getAllImage();

		HttpServletRequest re = ServletActionContext.getRequest();
		re.setAttribute("imageList", images);
		return "getallImageOK";
	}

	public String getImageById(int id) {
		image = imageDao.getImageById(id);
		if (image != null)
			return "getImageOK";
		else
			return "getImageError";
	}

	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public int getGoodId() {
		return goodId;
	}

	public void setGoodId(int goodId) {
		this.goodId = goodId;
	}

	public GoodsDao getGoodDao() {
		return goodDao;
	}

	public void setGoodDao(GoodsDao goodDao) {
		this.goodDao = goodDao;
	}
}
